package cn.fl.project.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.preloan.enums.LogBusinessTypeEnum;
import cn.fl.project.dto.PrjQuatPrpsCfgAiDTO;
import cn.fl.project.service.IPrjQuatPrpsCfgAiService;
import cn.fl.project.service.IProjectApiService;
import cn.fl.project.transform.PrjQuatPrpsCfgAiVOTransform;
import cn.fl.project.vo.PrjQuatPrpsCfgAiVO;
import com.alibaba.fastjson.JSON;
import com.lionbridge.log.ext.ExtSlf4jLogger;
import com.lionbridge.log.ext.ExtSlf4jLoggerBuilder;
import com.lionbridge.log.ext.LogTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 项目报价方案年利率配置表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Slf4j
@Service
public class PrjQuatPrpsCfgAiServiceImpl implements IPrjQuatPrpsCfgAiService {
	private static ExtSlf4jLogger logger = ExtSlf4jLoggerBuilder.create()
			.setLogger(log).setBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setSubBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setLogType(LogTypeEnum.BUSINESS).build().setKeywords("PrjQuatPrpsCfg");
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IProjectApiService projectApiService;
	
	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public PrjQuatPrpsCfgAiVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			logger.info("报价方案年利率配置,获取详情，请求参数id为空");
			return null;
		}
		PrjQuatPrpsCfgAiDTO dto = this.projectApiService.selectPrjQuatPrpsCfgAiById(id, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		return PrjQuatPrpsCfgAiVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjQuatPrpsCfgAiVO entity) throws BaseException{
		if(null == entity){
			logger.info("报价方案年利率配置,新增数据，请求参数为空");
			return null;
		}
		Long mainId = this.projectApiService.addPrjQuatPrpsCfgAi(PrjQuatPrpsCfgAiVOTransform.toDTO(entity));
		return mainId;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			logger.info("报价方案年利率配置,删除数据，请求参数id为空");
			return false;
		}
		//return this.prjQuatPrpsCfgAiApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
			logger.info("报价方案年利率配置,删除数据失败,请求参数为空");
			return false;
		};
		return this.projectApiService.removePrjQuatPrpsCfgAiByIds(ids);
    }

	@Override
	public boolean modify(PrjQuatPrpsCfgAiVO entity) throws BaseException{
		if(null == entity){
			logger.info("报价方案年利率配置,修改数据失败,请求参数为空entity:{}", JSON.toJSONString(entity));
			return false;
		}
		return this.projectApiService.modifyPrjQuatPrpsCfgAi(PrjQuatPrpsCfgAiVOTransform.toDTO(entity), CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.projectApiService.searchPrjQuatPrpsCfgAiListPage(page, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		page.setResultList(PrjQuatPrpsCfgAiVOTransform.toVOList((List<PrjQuatPrpsCfgAiDTO>)page.getResultList()));
		return page;
	}
	
	@Override
	public List<KeyValueVO> searchLsPdUntCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_PRJ_QUAT_LS_PD_UNT_CD);
	}
	
	@Override
	public List<KeyValueVO> searchYesOrNoCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_SYS_YES_NO_CD);
	}
	
	/**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	private List<KeyValueVO> getKvList(String grpCd){
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			logger.error("获取字典项组失败grpCd：{}",grpCd);
		} catch (Exception e) {
			logger.error("获取字典项组失败,系统错误");
		}
		return kvList;
	}
}
